package com.ywy.shiro;

import com.ywy.entity.User;
import com.ywy.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义Realm
 *
 * @author ywy
 * @date 2022/8/26 14:50
 */
public class MyRealm extends AuthorizingRealm {
    @Resource
    private UserService userService;

    /**
     * 授权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        User user = userService.getByUsername(username);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> roles = new HashSet<>();
        if ("ADMIN".equals(user.getRoleName())) {
            roles.add("ADMIN");

            info.addStringPermission("ADMIN");
            info.addStringPermission("RES_QUERY");
            info.addStringPermission("RES_EDIT");
            info.addStringPermission("RES_DELETE");
            info.addStringPermission("UPDATE_CHECK_STATUS");
            info.addStringPermission("UPDATE_HOT_STATUS");
            info.addStringPermission("RES_CHECK");
            info.addStringPermission("GEN_RES_INDEX");

            info.addStringPermission("RES_TYPE_QUERY");
            info.addStringPermission("RES_TYPE_EDIT");
            info.addStringPermission("RES_TYPE_DELETE");

            info.addStringPermission("USER_QUERY");
            info.addStringPermission("RESET_PASSWORD");
            info.addStringPermission("ADD_POINTS");
            info.addStringPermission("UPDATE_VIP_STATUS");
            info.addStringPermission("UPDATE_USER_STATUS");

            info.addStringPermission("LINK_QUERY");
            info.addStringPermission("LINK_EDIT");
            info.addStringPermission("LINK_DELETE");

            info.addStringPermission("COMMENT_QUERY");
            info.addStringPermission("COMMENT_EDIT");
            info.addStringPermission("COMMENT_DELETE");
        }
        info.setRoles(roles);
        return info;
    }

    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        User user = userService.getByUsername(username);
        if (user != null) {
            AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), "xx");
            return authcInfo;
        } else {
            return null;
        }
    }
}
